<?php
namespace app\controller;

use Swoole\Server;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server as WebsocketServer;
use think\Config;
use think\Request;
use think\swoole\contract\websocket\HandlerInterface;
use think\swoole\websocket\socketio\Packet;


class HandleTest implements HandlerInterface
{
    protected $server;
    protected $config;
    protected $webSocket;

    public function __construct(Server $server, Config $config, \think\swoole\Websocket $websocket)
    {
        $this->server = $server;
        $this->config = $config;
        $this->webSocket = $websocket;
    }

    public function onOpen($fd, Request $request)
    {
        //echo 'onOpen'.PHP_EOL;
    }

    public function onMessage(Frame $frame)
    {
        $fd = $frame->fd;
        echo 'onMessage==='.$fd,PHP_EOL;
        $packet = $frame->data;
        $data = json_decode($packet, true);
        var_dump($data);
        $this->server->push($fd, '6666666');

        //$uid = $data['fromUid'];
        //$this->webSocket->join($uid);
        //print_r($this->webSocket);
        //print_r($this->server->connection_info($fd));
        //print_r($this->server->connections);
        //print_r($this->server->connection_list(0));
        //print_r($this->server->getClientList(0));
        //print_r($this->server->getClientInfo($fd));
        /*
            Array
            (
                [uid] => 10
                [websocket_status] => 3
                [server_port] => 2348
                [server_fd] => 5
                [socket_fd] => 21
                [socket_type] => 1
                [remote_port] => 54794
                [remote_ip] => 113.110.225.114
                [reactor_id] => 1
                [connect_time] => 1584440190
                [last_time] => 1584440190
                [close_errno] => 0
            )
            */

        /*if($data['type'] == 'bind'){
            $this->server->bind($fd, $uid);
            $this->server->push($fd, '绑定成功');
        }elseif($data['type'] == 'text'){
            $this->server->push($fd, $data['msg']);
        }else{
            $this->server->push($fd, 'unknowmsg');
        }*/

        return true;
    }

    public function onClose($fd, $reactorId)
    {
        return;
    }

    public static function httpWorker($socket)
    {

        //$server = new Server('0.0.0.0', '2123');
        /*$inner_http_worker = new Server('0.0.0.0', '2123');
        $inner_http_worker->onMessage = function($http_connection, $data) use ($socket){
            global $uidConnectionMap;
            $_POST = $_POST ? $_POST : $_GET;
            // 推送数据的url格式 type=publish&to=uid&content=xxxx
            switch(@$_POST['type']){
                case 'publish':
                    $toUid = @$_POST['toUid'];
                    $_POST['content'] = htmlspecialchars(@$_POST['content']);

                    if($toUid){
                        $socket->to($toUid)->emit('new_msg', $_POST['content']);
                    }else{
                        $socket->emit('new_msg', $_POST['content']);
                    }

                    if($toUid && !isset($uidConnectionMap[$toUid])){
                        return $http_connection->send('ok');
                    }else{
                        return $http_connection->send('offline');
                    }
                    break;
            }
            return $http_connection->send('fail');
        };

        // 执行监听
        $inner_http_worker->listen();*/
    }
}